<?php

namespace App\Http\Controllers\ProjectManager;

use App\Http\Controllers\Controller;
use App\Http\Controllers\Utils\ResponseFactory;
use App\Http\Requests\projectManager\addItemRequest;
use App\Http\Requests\projectManager\UpdateItemRequest;
use App\Models\ProjectManager\IDEType;
use App\Models\ProjectManager\StoreItems;
use Carbon\Carbon;
use Carbon\Traits\Date;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Ramsey\Uuid\Type\Time;
use function GuzzleHttp\Promise\all;

class ProjectManagerController extends Controller
{
    //

    public function addItem(addItemRequest $request)
    {
        $user = auth()->user();
        $data = $request->all();
        if (StoreItems::all()->where('local_path', '=', $data["local_path"])->count() > 0) {
            return ResponseFactory::simpleResponse(false, "当前项目已添加");
        }
        $item = [
            "local_path" => $data["local_path"],
            "create_time" => Carbon::now()->toDateTimeString(),
            "update_time" => Carbon::now()->toDateTimeString(),
            "project_name" => $data["project_name"],
            'userId' => $user["id"],
            "project_name" => $data["project_name"],
        ];
        if ($request->has("remark")) {
            $item["remark"] = $data["remark"];
        }
        $store_item = StoreItems::create($item);
        $IdeType = new IDEType([
            'IDE_Name' => $data["IDE_Name"],
            'IDE_Version' => $data["IDE_Version"],
            'IDE_Path' => $data["IDE_Path"],
            'item_id' => $store_item["id"],
        ]);

        $store_item->IdeType()->save($IdeType);
        return ResponseFactory::simpleResponse(true, "项目信息记录成功");
    }

    public function SelectStoreItems()
    {
        $user = auth()->user();
        $StoreItems = StoreItems::all()->where('userId', $user["id"]);
        $rows = [];
        foreach ($StoreItems as $item) {
            $idetypes = $item->IdeType;
            $rows[] = $item;
        }
        return ResponseFactory::responseRows($rows);
    }

    public function getIdeTypes(): JsonResponse
    {
        $userId = auth()->user()["id"];
        $rows = [];
        $StoreItems = StoreItems::all()->where('userId', $userId);
        foreach ($StoreItems as $item) {
            $rows[] = IDEType::all()->where('item_id', $item["id"]);
        }
        return ResponseFactory::responseRows($rows);
    }

    public function UpdateItem(UpdateItemRequest $request): JsonResponse
    {
        $params = $request->all("project_name", "local_path", "update_time", "remark");
        $id = $request["id"];
        $item = StoreItems::all()->find($id);
        foreach ($params as $key => $value) {
            if ($value != null) {
                $item[$key] = $value;
            }
        }
        $item->save();
        return ResponseFactory::simpleResponse(true, "操作成功", 200);
    }

    public function DeleteItem(int $id)
    {
        $row = StoreItems::destroy($id);
        if ($row > 0) {
            return ResponseFactory::simpleResponse(true, "删除成功", 200);
        } else {
            return ResponseFactory::simpleResponse(false, "删除失败", 400);
        }
    }

    public function getUserProjectMessage()
    {
        $storeItems = StoreItems::all();
        $TotalProjectItemNums = StoreItems::all()->count();
        $RecentItemNums = 0;
        $LongTimeItemNums = 0;
        foreach ($storeItems as $key => $value) {
            $diffTime = carbon::parse(Carbon::now())->diffInDays($value["update_time"], true);
            if ($diffTime < 2) {
                $RecentItemNums++;
            } else if ($diffTime > 30) {
                $LongTimeItemNums++;
            }
        }
        $result = [
            "TotalProjectItemNums" => $TotalProjectItemNums,
            "RecentItemNums" => $RecentItemNums,
            "LongTimeItemNums" => $LongTimeItemNums,
        ];
        return ResponseFactory::responseData($result);
    }
}
